package org.jeecg.modules.crm.controller;

import org.jeecg.common.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import java.util.Arrays;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.crm.entity.SubSalesScheme;
import org.jeecg.modules.crm.entity.AccumulatedSales;
import org.jeecg.modules.crm.entity.AgentProjectScheme;
import org.jeecg.modules.crm.entity.AgentSpecialOverriding;
import org.jeecg.modules.crm.entity.AgentPromote;
import org.jeecg.modules.crm.entity.WithholdingTax;
import org.jeecg.modules.crm.entity.Agent;
import org.jeecg.modules.crm.service.IAgentService;
import org.jeecg.modules.crm.service.ISubSalesSchemeService;
import org.jeecg.modules.crm.service.IAccumulatedSalesService;
import org.jeecg.modules.crm.service.IAgentProjectSchemeService;
import org.jeecg.modules.crm.service.IAgentSpecialOverridingService;
import org.jeecg.modules.crm.service.IAgentPromoteService;
import org.jeecg.modules.crm.service.IWithholdingTaxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

 /**
 * @Description: Agent
 * @Author: jeecg-boot
 * @Date:   2025-04-13
 * @Version: V1.0
 */
@Api(tags="Agent")
@RestController
@RequestMapping("/crm/agent")
@Slf4j
public class AgentController extends JeecgController<Agent, IAgentService> {

	@Autowired
	private IAgentService agentService;

	@Autowired
	private ISubSalesSchemeService subSalesSchemeService;

	@Autowired
	private IAccumulatedSalesService accumulatedSalesService;

	@Autowired
	private IAgentProjectSchemeService agentProjectSchemeService;

	@Autowired
	private IAgentSpecialOverridingService agentSpecialOverridingService;

	@Autowired
	private IAgentPromoteService agentPromoteService;

	@Autowired
	private IWithholdingTaxService withholdingTaxService;


	/*---------------------------------主表处理-begin-------------------------------------*/

	/**
	 * 分页列表查询
	 * @param agent
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "Agent-分页列表查询")
	@ApiOperation(value="Agent-分页列表查询", notes="Agent-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<Agent>> queryPageList(Agent agent,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Agent> queryWrapper = QueryGenerator.initQueryWrapper(agent, req.getParameterMap());
		Page<Agent> page = new Page<Agent>(pageNo, pageSize);
		IPage<Agent> pageList = agentService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
     *   添加
     * @param agent
     * @return
     */
    @AutoLog(value = "Agent-添加")
    @ApiOperation(value="Agent-添加", notes="Agent-添加")
    //@RequiresPermissions("org.jeecg.modules:agent:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody Agent agent) {
        agentService.save(agent);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     * @param agent
     * @return
     */
    @AutoLog(value = "Agent-编辑")
    @ApiOperation(value="Agent-编辑", notes="Agent-编辑")
    //@RequiresPermissions("org.jeecg.modules:agent:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestBody Agent agent) {
        agentService.updateById(agent);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @AutoLog(value = "Agent-通过id删除")
    @ApiOperation(value="Agent-通过id删除", notes="Agent-通过id删除")
    //@RequiresPermissions("org.jeecg.modules:agent:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        agentService.delMain(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @AutoLog(value = "Agent-批量删除")
    @ApiOperation(value="Agent-批量删除", notes="Agent-批量删除")
    //@RequiresPermissions("org.jeecg.modules:agent:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.agentService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 导出
     * @return
     */
    //@RequiresPermissions("org.jeecg.modules:agent:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Agent agent) {
        return super.exportXls(request, agent, Agent.class, "Agent");
    }

    /**
     * 导入
     * @return
     */
    //@RequiresPermissions("org.jeecg.modules:agent:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Agent.class);
    }
	/*---------------------------------主表处理-end-------------------------------------*/
	

    /*--------------------------------子表处理-Sub Sales Scheme-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return
	 */
	//@AutoLog(value = "Sub Sales Scheme-通过主表ID查询")
	@ApiOperation(value="Sub Sales Scheme-通过主表ID查询", notes="Sub Sales Scheme-通过主表ID查询")
	@GetMapping(value = "/listSubSalesSchemeByMainId")
    public Result<IPage<SubSalesScheme>> listSubSalesSchemeByMainId(SubSalesScheme subSalesScheme,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<SubSalesScheme> queryWrapper = QueryGenerator.initQueryWrapper(subSalesScheme, req.getParameterMap());
        Page<SubSalesScheme> page = new Page<SubSalesScheme>(pageNo, pageSize);
        IPage<SubSalesScheme> pageList = subSalesSchemeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

	/**
	 * 添加
	 * @param subSalesScheme
	 * @return
	 */
	@AutoLog(value = "Sub Sales Scheme-添加")
	@ApiOperation(value="Sub Sales Scheme-添加", notes="Sub Sales Scheme-添加")
	@PostMapping(value = "/addSubSalesScheme")
	public Result<String> addSubSalesScheme(@RequestBody SubSalesScheme subSalesScheme) {
		subSalesSchemeService.save(subSalesScheme);
		return Result.OK("添加成功！");
	}

    /**
	 * 编辑
	 * @param subSalesScheme
	 * @return
	 */
	@AutoLog(value = "Sub Sales Scheme-编辑")
	@ApiOperation(value="Sub Sales Scheme-编辑", notes="Sub Sales Scheme-编辑")
	@RequestMapping(value = "/editSubSalesScheme", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> editSubSalesScheme(@RequestBody SubSalesScheme subSalesScheme) {
		subSalesSchemeService.updateById(subSalesScheme);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "Sub Sales Scheme-通过id删除")
	@ApiOperation(value="Sub Sales Scheme-通过id删除", notes="Sub Sales Scheme-通过id删除")
	@DeleteMapping(value = "/deleteSubSalesScheme")
	public Result<String> deleteSubSalesScheme(@RequestParam(name="id",required=true) String id) {
		subSalesSchemeService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "Sub Sales Scheme-批量删除")
	@ApiOperation(value="Sub Sales Scheme-批量删除", notes="Sub Sales Scheme-批量删除")
	@DeleteMapping(value = "/deleteBatchSubSalesScheme")
	public Result<String> deleteBatchSubSalesScheme(@RequestParam(name="ids",required=true) String ids) {
	    this.subSalesSchemeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportSubSalesScheme")
    public ModelAndView exportSubSalesScheme(HttpServletRequest request, SubSalesScheme subSalesScheme) {
		 // Step.1 组装查询条件
		 QueryWrapper<SubSalesScheme> queryWrapper = QueryGenerator.initQueryWrapper(subSalesScheme, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<SubSalesScheme> pageList = subSalesSchemeService.list(queryWrapper);
		 List<SubSalesScheme> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 //此处设置的filename无效,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.FILE_NAME, "Sub Sales Scheme");
		 mv.addObject(NormalExcelConstants.CLASS, SubSalesScheme.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("Sub Sales Scheme报表", "导出人:" + sysUser.getRealname(), "Sub Sales Scheme"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importSubSalesScheme/{mainId}")
    public Result<?> importSubSalesScheme(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
       // 获取上传文件对象
			 MultipartFile file = entity.getValue();
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<SubSalesScheme> list = ExcelImportUtil.importExcel(file.getInputStream(), SubSalesScheme.class, params);
				 for (SubSalesScheme temp : list) {
                    temp.setMainId(mainId);
				 }
				 long start = System.currentTimeMillis();
				 subSalesSchemeService.saveBatch(list);
				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				 return Result.OK("文件导入成功！数据行数：" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-Sub Sales Scheme-end----------------------------------------------*/

    /*--------------------------------子表处理-Accumulated Sales-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return
	 */
	//@AutoLog(value = "Accumulated Sales-通过主表ID查询")
	@ApiOperation(value="Accumulated Sales-通过主表ID查询", notes="Accumulated Sales-通过主表ID查询")
	@GetMapping(value = "/listAccumulatedSalesByMainId")
    public Result<IPage<AccumulatedSales>> listAccumulatedSalesByMainId(AccumulatedSales accumulatedSales,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<AccumulatedSales> queryWrapper = QueryGenerator.initQueryWrapper(accumulatedSales, req.getParameterMap());
        Page<AccumulatedSales> page = new Page<AccumulatedSales>(pageNo, pageSize);
        IPage<AccumulatedSales> pageList = accumulatedSalesService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

	/**
	 * 添加
	 * @param accumulatedSales
	 * @return
	 */
	@AutoLog(value = "Accumulated Sales-添加")
	@ApiOperation(value="Accumulated Sales-添加", notes="Accumulated Sales-添加")
	@PostMapping(value = "/addAccumulatedSales")
	public Result<String> addAccumulatedSales(@RequestBody AccumulatedSales accumulatedSales) {
		accumulatedSalesService.save(accumulatedSales);
		return Result.OK("添加成功！");
	}

    /**
	 * 编辑
	 * @param accumulatedSales
	 * @return
	 */
	@AutoLog(value = "Accumulated Sales-编辑")
	@ApiOperation(value="Accumulated Sales-编辑", notes="Accumulated Sales-编辑")
	@RequestMapping(value = "/editAccumulatedSales", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> editAccumulatedSales(@RequestBody AccumulatedSales accumulatedSales) {
		accumulatedSalesService.updateById(accumulatedSales);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "Accumulated Sales-通过id删除")
	@ApiOperation(value="Accumulated Sales-通过id删除", notes="Accumulated Sales-通过id删除")
	@DeleteMapping(value = "/deleteAccumulatedSales")
	public Result<String> deleteAccumulatedSales(@RequestParam(name="id",required=true) String id) {
		accumulatedSalesService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "Accumulated Sales-批量删除")
	@ApiOperation(value="Accumulated Sales-批量删除", notes="Accumulated Sales-批量删除")
	@DeleteMapping(value = "/deleteBatchAccumulatedSales")
	public Result<String> deleteBatchAccumulatedSales(@RequestParam(name="ids",required=true) String ids) {
	    this.accumulatedSalesService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportAccumulatedSales")
    public ModelAndView exportAccumulatedSales(HttpServletRequest request, AccumulatedSales accumulatedSales) {
		 // Step.1 组装查询条件
		 QueryWrapper<AccumulatedSales> queryWrapper = QueryGenerator.initQueryWrapper(accumulatedSales, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<AccumulatedSales> pageList = accumulatedSalesService.list(queryWrapper);
		 List<AccumulatedSales> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 //此处设置的filename无效,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.FILE_NAME, "Accumulated Sales");
		 mv.addObject(NormalExcelConstants.CLASS, AccumulatedSales.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("Accumulated Sales报表", "导出人:" + sysUser.getRealname(), "Accumulated Sales"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importAccumulatedSales/{mainId}")
    public Result<?> importAccumulatedSales(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
       // 获取上传文件对象
			 MultipartFile file = entity.getValue();
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<AccumulatedSales> list = ExcelImportUtil.importExcel(file.getInputStream(), AccumulatedSales.class, params);
				 for (AccumulatedSales temp : list) {
                    temp.setMainId(mainId);
				 }
				 long start = System.currentTimeMillis();
				 accumulatedSalesService.saveBatch(list);
				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				 return Result.OK("文件导入成功！数据行数：" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-Accumulated Sales-end----------------------------------------------*/

    /*--------------------------------子表处理-Project Scheme-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return
	 */
	//@AutoLog(value = "Project Scheme-通过主表ID查询")
	@ApiOperation(value="Project Scheme-通过主表ID查询", notes="Project Scheme-通过主表ID查询")
	@GetMapping(value = "/listAgentProjectSchemeByMainId")
    public Result<IPage<AgentProjectScheme>> listAgentProjectSchemeByMainId(AgentProjectScheme agentProjectScheme,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<AgentProjectScheme> queryWrapper = QueryGenerator.initQueryWrapper(agentProjectScheme, req.getParameterMap());
        Page<AgentProjectScheme> page = new Page<AgentProjectScheme>(pageNo, pageSize);
        IPage<AgentProjectScheme> pageList = agentProjectSchemeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

	/**
	 * 添加
	 * @param agentProjectScheme
	 * @return
	 */
	@AutoLog(value = "Project Scheme-添加")
	@ApiOperation(value="Project Scheme-添加", notes="Project Scheme-添加")
	@PostMapping(value = "/addAgentProjectScheme")
	public Result<String> addAgentProjectScheme(@RequestBody AgentProjectScheme agentProjectScheme) {
		agentProjectSchemeService.save(agentProjectScheme);
		return Result.OK("添加成功！");
	}

    /**
	 * 编辑
	 * @param agentProjectScheme
	 * @return
	 */
	@AutoLog(value = "Project Scheme-编辑")
	@ApiOperation(value="Project Scheme-编辑", notes="Project Scheme-编辑")
	@RequestMapping(value = "/editAgentProjectScheme", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> editAgentProjectScheme(@RequestBody AgentProjectScheme agentProjectScheme) {
		agentProjectSchemeService.updateById(agentProjectScheme);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "Project Scheme-通过id删除")
	@ApiOperation(value="Project Scheme-通过id删除", notes="Project Scheme-通过id删除")
	@DeleteMapping(value = "/deleteAgentProjectScheme")
	public Result<String> deleteAgentProjectScheme(@RequestParam(name="id",required=true) String id) {
		agentProjectSchemeService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "Project Scheme-批量删除")
	@ApiOperation(value="Project Scheme-批量删除", notes="Project Scheme-批量删除")
	@DeleteMapping(value = "/deleteBatchAgentProjectScheme")
	public Result<String> deleteBatchAgentProjectScheme(@RequestParam(name="ids",required=true) String ids) {
	    this.agentProjectSchemeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportAgentProjectScheme")
    public ModelAndView exportAgentProjectScheme(HttpServletRequest request, AgentProjectScheme agentProjectScheme) {
		 // Step.1 组装查询条件
		 QueryWrapper<AgentProjectScheme> queryWrapper = QueryGenerator.initQueryWrapper(agentProjectScheme, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<AgentProjectScheme> pageList = agentProjectSchemeService.list(queryWrapper);
		 List<AgentProjectScheme> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 //此处设置的filename无效,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.FILE_NAME, "Project Scheme");
		 mv.addObject(NormalExcelConstants.CLASS, AgentProjectScheme.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("Project Scheme报表", "导出人:" + sysUser.getRealname(), "Project Scheme"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importAgentProjectScheme/{mainId}")
    public Result<?> importAgentProjectScheme(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
       // 获取上传文件对象
			 MultipartFile file = entity.getValue();
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<AgentProjectScheme> list = ExcelImportUtil.importExcel(file.getInputStream(), AgentProjectScheme.class, params);
				 for (AgentProjectScheme temp : list) {
                    temp.setMainId(mainId);
				 }
				 long start = System.currentTimeMillis();
				 agentProjectSchemeService.saveBatch(list);
				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				 return Result.OK("文件导入成功！数据行数：" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-Project Scheme-end----------------------------------------------*/

    /*--------------------------------子表处理-Special Overriding-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return
	 */
	//@AutoLog(value = "Special Overriding-通过主表ID查询")
	@ApiOperation(value="Special Overriding-通过主表ID查询", notes="Special Overriding-通过主表ID查询")
	@GetMapping(value = "/listAgentSpecialOverridingByMainId")
    public Result<IPage<AgentSpecialOverriding>> listAgentSpecialOverridingByMainId(AgentSpecialOverriding agentSpecialOverriding,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<AgentSpecialOverriding> queryWrapper = QueryGenerator.initQueryWrapper(agentSpecialOverriding, req.getParameterMap());
        Page<AgentSpecialOverriding> page = new Page<AgentSpecialOverriding>(pageNo, pageSize);
        IPage<AgentSpecialOverriding> pageList = agentSpecialOverridingService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

	/**
	 * 添加
	 * @param agentSpecialOverriding
	 * @return
	 */
	@AutoLog(value = "Special Overriding-添加")
	@ApiOperation(value="Special Overriding-添加", notes="Special Overriding-添加")
	@PostMapping(value = "/addAgentSpecialOverriding")
	public Result<String> addAgentSpecialOverriding(@RequestBody AgentSpecialOverriding agentSpecialOverriding) {
		agentSpecialOverridingService.save(agentSpecialOverriding);
		return Result.OK("添加成功！");
	}

    /**
	 * 编辑
	 * @param agentSpecialOverriding
	 * @return
	 */
	@AutoLog(value = "Special Overriding-编辑")
	@ApiOperation(value="Special Overriding-编辑", notes="Special Overriding-编辑")
	@RequestMapping(value = "/editAgentSpecialOverriding", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> editAgentSpecialOverriding(@RequestBody AgentSpecialOverriding agentSpecialOverriding) {
		agentSpecialOverridingService.updateById(agentSpecialOverriding);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "Special Overriding-通过id删除")
	@ApiOperation(value="Special Overriding-通过id删除", notes="Special Overriding-通过id删除")
	@DeleteMapping(value = "/deleteAgentSpecialOverriding")
	public Result<String> deleteAgentSpecialOverriding(@RequestParam(name="id",required=true) String id) {
		agentSpecialOverridingService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "Special Overriding-批量删除")
	@ApiOperation(value="Special Overriding-批量删除", notes="Special Overriding-批量删除")
	@DeleteMapping(value = "/deleteBatchAgentSpecialOverriding")
	public Result<String> deleteBatchAgentSpecialOverriding(@RequestParam(name="ids",required=true) String ids) {
	    this.agentSpecialOverridingService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportAgentSpecialOverriding")
    public ModelAndView exportAgentSpecialOverriding(HttpServletRequest request, AgentSpecialOverriding agentSpecialOverriding) {
		 // Step.1 组装查询条件
		 QueryWrapper<AgentSpecialOverriding> queryWrapper = QueryGenerator.initQueryWrapper(agentSpecialOverriding, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<AgentSpecialOverriding> pageList = agentSpecialOverridingService.list(queryWrapper);
		 List<AgentSpecialOverriding> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 //此处设置的filename无效,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.FILE_NAME, "Special Overriding");
		 mv.addObject(NormalExcelConstants.CLASS, AgentSpecialOverriding.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("Special Overriding报表", "导出人:" + sysUser.getRealname(), "Special Overriding"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importAgentSpecialOverriding/{mainId}")
    public Result<?> importAgentSpecialOverriding(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
       // 获取上传文件对象
			 MultipartFile file = entity.getValue();
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<AgentSpecialOverriding> list = ExcelImportUtil.importExcel(file.getInputStream(), AgentSpecialOverriding.class, params);
				 for (AgentSpecialOverriding temp : list) {
                    temp.setMainId(mainId);
				 }
				 long start = System.currentTimeMillis();
				 agentSpecialOverridingService.saveBatch(list);
				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				 return Result.OK("文件导入成功！数据行数：" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-Special Overriding-end----------------------------------------------*/

    /*--------------------------------子表处理-Agent Promote-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return
	 */
	//@AutoLog(value = "Agent Promote-通过主表ID查询")
	@ApiOperation(value="Agent Promote-通过主表ID查询", notes="Agent Promote-通过主表ID查询")
	@GetMapping(value = "/listAgentPromoteByMainId")
    public Result<IPage<AgentPromote>> listAgentPromoteByMainId(AgentPromote agentPromote,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<AgentPromote> queryWrapper = QueryGenerator.initQueryWrapper(agentPromote, req.getParameterMap());
        Page<AgentPromote> page = new Page<AgentPromote>(pageNo, pageSize);
        IPage<AgentPromote> pageList = agentPromoteService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

	/**
	 * 添加
	 * @param agentPromote
	 * @return
	 */
	@AutoLog(value = "Agent Promote-添加")
	@ApiOperation(value="Agent Promote-添加", notes="Agent Promote-添加")
	@PostMapping(value = "/addAgentPromote")
	public Result<String> addAgentPromote(@RequestBody AgentPromote agentPromote) {
		agentPromoteService.save(agentPromote);
		return Result.OK("添加成功！");
	}

    /**
	 * 编辑
	 * @param agentPromote
	 * @return
	 */
	@AutoLog(value = "Agent Promote-编辑")
	@ApiOperation(value="Agent Promote-编辑", notes="Agent Promote-编辑")
	@RequestMapping(value = "/editAgentPromote", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> editAgentPromote(@RequestBody AgentPromote agentPromote) {
		agentPromoteService.updateById(agentPromote);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "Agent Promote-通过id删除")
	@ApiOperation(value="Agent Promote-通过id删除", notes="Agent Promote-通过id删除")
	@DeleteMapping(value = "/deleteAgentPromote")
	public Result<String> deleteAgentPromote(@RequestParam(name="id",required=true) String id) {
		agentPromoteService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "Agent Promote-批量删除")
	@ApiOperation(value="Agent Promote-批量删除", notes="Agent Promote-批量删除")
	@DeleteMapping(value = "/deleteBatchAgentPromote")
	public Result<String> deleteBatchAgentPromote(@RequestParam(name="ids",required=true) String ids) {
	    this.agentPromoteService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportAgentPromote")
    public ModelAndView exportAgentPromote(HttpServletRequest request, AgentPromote agentPromote) {
		 // Step.1 组装查询条件
		 QueryWrapper<AgentPromote> queryWrapper = QueryGenerator.initQueryWrapper(agentPromote, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<AgentPromote> pageList = agentPromoteService.list(queryWrapper);
		 List<AgentPromote> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 //此处设置的filename无效,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.FILE_NAME, "Agent Promote");
		 mv.addObject(NormalExcelConstants.CLASS, AgentPromote.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("Agent Promote报表", "导出人:" + sysUser.getRealname(), "Agent Promote"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importAgentPromote/{mainId}")
    public Result<?> importAgentPromote(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
       // 获取上传文件对象
			 MultipartFile file = entity.getValue();
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<AgentPromote> list = ExcelImportUtil.importExcel(file.getInputStream(), AgentPromote.class, params);
				 for (AgentPromote temp : list) {
                    temp.setMainId(mainId);
				 }
				 long start = System.currentTimeMillis();
				 agentPromoteService.saveBatch(list);
				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				 return Result.OK("文件导入成功！数据行数：" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-Agent Promote-end----------------------------------------------*/

    /*--------------------------------子表处理-Withholding Tax-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return
	 */
	//@AutoLog(value = "Withholding Tax-通过主表ID查询")
	@ApiOperation(value="Withholding Tax-通过主表ID查询", notes="Withholding Tax-通过主表ID查询")
	@GetMapping(value = "/listWithholdingTaxByMainId")
    public Result<IPage<WithholdingTax>> listWithholdingTaxByMainId(WithholdingTax withholdingTax,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<WithholdingTax> queryWrapper = QueryGenerator.initQueryWrapper(withholdingTax, req.getParameterMap());
        Page<WithholdingTax> page = new Page<WithholdingTax>(pageNo, pageSize);
        IPage<WithholdingTax> pageList = withholdingTaxService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

	/**
	 * 添加
	 * @param withholdingTax
	 * @return
	 */
	@AutoLog(value = "Withholding Tax-添加")
	@ApiOperation(value="Withholding Tax-添加", notes="Withholding Tax-添加")
	@PostMapping(value = "/addWithholdingTax")
	public Result<String> addWithholdingTax(@RequestBody WithholdingTax withholdingTax) {
		withholdingTaxService.save(withholdingTax);
		return Result.OK("添加成功！");
	}

    /**
	 * 编辑
	 * @param withholdingTax
	 * @return
	 */
	@AutoLog(value = "Withholding Tax-编辑")
	@ApiOperation(value="Withholding Tax-编辑", notes="Withholding Tax-编辑")
	@RequestMapping(value = "/editWithholdingTax", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> editWithholdingTax(@RequestBody WithholdingTax withholdingTax) {
		withholdingTaxService.updateById(withholdingTax);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "Withholding Tax-通过id删除")
	@ApiOperation(value="Withholding Tax-通过id删除", notes="Withholding Tax-通过id删除")
	@DeleteMapping(value = "/deleteWithholdingTax")
	public Result<String> deleteWithholdingTax(@RequestParam(name="id",required=true) String id) {
		withholdingTaxService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "Withholding Tax-批量删除")
	@ApiOperation(value="Withholding Tax-批量删除", notes="Withholding Tax-批量删除")
	@DeleteMapping(value = "/deleteBatchWithholdingTax")
	public Result<String> deleteBatchWithholdingTax(@RequestParam(name="ids",required=true) String ids) {
	    this.withholdingTaxService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportWithholdingTax")
    public ModelAndView exportWithholdingTax(HttpServletRequest request, WithholdingTax withholdingTax) {
		 // Step.1 组装查询条件
		 QueryWrapper<WithholdingTax> queryWrapper = QueryGenerator.initQueryWrapper(withholdingTax, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<WithholdingTax> pageList = withholdingTaxService.list(queryWrapper);
		 List<WithholdingTax> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 //此处设置的filename无效,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.FILE_NAME, "Withholding Tax");
		 mv.addObject(NormalExcelConstants.CLASS, WithholdingTax.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("Withholding Tax报表", "导出人:" + sysUser.getRealname(), "Withholding Tax"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importWithholdingTax/{mainId}")
    public Result<?> importWithholdingTax(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
       // 获取上传文件对象
			 MultipartFile file = entity.getValue();
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<WithholdingTax> list = ExcelImportUtil.importExcel(file.getInputStream(), WithholdingTax.class, params);
				 for (WithholdingTax temp : list) {
                    temp.setMainId(mainId);
				 }
				 long start = System.currentTimeMillis();
				 withholdingTaxService.saveBatch(list);
				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				 return Result.OK("文件导入成功！数据行数：" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-Withholding Tax-end----------------------------------------------*/




}
